<div id="form"></div>
<script>
var datatypes={{JSON:datatypes}};
var gfields;
var gstates;

function datatype_changed(e)
{
	var a=e.data;
	var dt_id=a.tag.attr("options")[a.tag.attr("selectedIndex")].value;
	a.grid.saveChangedRows();
	a.row.rowClass=find_datatype(dt_id).editclass||"field";
	a.grid.renderRow(a.rownum,true);
}

function add_field_group_clicked() {
	gfields.ajajform_grid("insertRow",null,{rowClass: "field-group",cells:["",null,"",1,1,null]});
}

function find_datatype(dtid) {
	for (var i=0;i<datatypes.length;i++) {
		if (datatypes[i].id==dtid) return datatypes[i];
	}
}

function add_field_clicked() {
	insert_field({datatype_id:datatypes[0].id});
}

function insert_field(fi) {
	var dt=find_datatype(fi.datatype_id);
	var cells=[fi.name,fi.code,fi.cssclass,null,null,fi.datatype_id,fi.size,fi.tablename];
	var cl=dt.editclass||"field";
	gfields.ajajform_grid("insertRow",null,{rowClass: cl, cells: cells, id: fi.id, level: 1});
}

function del_fields_clicked() {
	gfields.ajajform_grid("deleteSelectedRownums");
}

var frm=$("#form").ajajform({
	header: "Редактирование типа транзакции",
	submit: {
		url: '{{action}}',
		value: "Сохранить",
		ajaj: true,
		params: {mode: "save",type_id: '{{ttype.id}}',ajaj: 1},
	},
	footerButtons: [
		{value:"Добавить состояние", click: function() {gstates.ajajform_grid("insertRow",null,{rowClass: "state",cells:["","","","","",""]});}},
		{value:"Удалить выбранные состояния", click: function() {gstates.ajajform_grid("deleteSelectedRownums");}},
		{value:"Добавить группу полей", click: add_field_group_clicked},
		{value:"Добавить поле", click: add_field_clicked},
		{value:"Удалить выбранные поля", click: del_fields_clicked}
	]
});
frm.ajajform("insertWidget",null,null,{
	type: "textedit",
	name: "code",
	label: "Code",
	validate: function(x) {if (!x || x.length==0) return "Пустой код";},
	width: 500,
	value: "{{ttype.code}}"
});
frm.ajajform("insertWidget",null,null,{
	type: "textedit",
	name: "name",
	label: "Название",
	validate: function(x) {if (!x || x.length==0) return "Пустое имя";},
	width: 500,
	value: "{{ttype.name}}"
});
frm.ajajform("insertWidget",null,null,{
	type: "checkbox",
	name: "can_embed",
	label: "Встраивает в себя",
	width: 350,
	value: "{{ttype.can_embed}}"
});
frm.ajajform("insertWidget",null,null,{
	type: "checkbox",
	name: "is_embeded",
	label: "Можно встраивать",
	width: 350,
	value: "{{ttype.is_embeded}}"
});
var roles={{JSON:roles}};
gstates=frm.ajajform("insertWidget",null,null,{
	type: "grid",
	rowSelection: true,
	title: "Состояния",
	name: "states",
	mode: "form-edit-full",
	rowClasses: [{
		name:"state",
		columns: [
			{name: "name", label: "Название", width: 250},
			{name: "code", label: "Code", width: 120},
			{name: "role_cr_id", label: "Роль - создание", type: "select", width: 150, source: roles},
			{name: "role_edit_id", label: "Роль - редактирование", type: "select", width: 150, source: roles},
			{name: "role_up_to_id", label: "Роль - продвижение до", type: "select", width: 150, source: roles},
			{name: "role_del_id", label: "Роль - удаление", type: "select", width: 150, source: roles}
		]
	}]
});

gfields=frm.ajajform("insertWidget",null,null,{
	type: "grid",
	rowSelection: true,
	title: "Поля",
	name: "fields",
	mode: "form-edit-full",
	rowClasses: [
		{
			name: "field-group",
			columns: [
				{name: "name", label: "Название", width: 250},
				{name: "code", label: "Код", width: 120, type: "none"},
				{name: "cssclass", label: "CSS-класс", width: 80},
				{name: "opened", label: "Откр", width: 40, type: "checkbox" },
				{name: "opened_embeded", label: "Встр отк", width: 50, type: "checkbox" },
				{name: "datatype_id", label: "Тип", width: 150, type: "none" },
				{name: "size", label: "Размер", width: 90, type: "none" },
				{name: "tablename", label: "Таблица", width: 90, type: "none" }
			],
			cssclass: "field-group"
		},
		{
			name:"field",
			columns: [
				{name: "name", label: "Название"},
				{name: "code", label: "Code"},
				{name: "cssclass", label: "CSS-класс"},
				{name: "opened", type: "none"},
				{name: "opened_embeded", type: "none" },
				{name: "datatype_id", label: "Тип", type: "select", source: datatypes,change: datatype_changed },
				{name: "size", type: "none" },
				{name: "tablename", type: "none" }
			],
			cssclass: "field"
		},
		{
			name:"field_number",
			columns: [
				{name: "name", label: "Название"},
				{name: "code", label: "Code"},
				{name: "cssclass", label: "CSS-класс"},
				{name: "opened", type: "none"},
				{name: "opened_embeded", type: "none" },
				{name: "datatype_id", label: "Тип", type: "select", source: datatypes,change: datatype_changed },
				{name: "size", label: "Размер" },
				{name: "tablename", type: "none" }
			],
			cssclass: "field"
		},
		{
			name:"field_varchar",
			columns: [
				{name: "name", label: "Название"},
				{name: "code", label: "Code"},
				{name: "cssclass", label: "CSS-класс"},
				{name: "opened", type: "none"},
				{name: "opened_embeded", type: "none" },
				{name: "datatype_id", label: "Тип", type: "select", source: datatypes,change: datatype_changed },
				{name: "size", label: "Размер" },
				{name: "tablename", type: "none" }
			],
			cssclass: "field"
		},
		{
			name:"field_fk",
			columns: [
				{name: "name", label: "Название"},
				{name: "code", label: "Code"},
				{name: "cssclass", label: "CSS-класс"},
				{name: "opened", type: "none"},
				{name: "opened_embeded", type: "none" },
				{name: "datatype_id", label: "Тип", type: "select", source: datatypes,change: datatype_changed },
				{name: "size", type: "none" },
				{name: "tablename", label: "Таблица" }
			],
			cssclass: "field"
		},
		{
			name:"field_account",
			columns: [
				{name: "name", label: "Название"},
				{name: "code", label: "Code"},
				{name: "cssclass", label: "CSS-класс"},
				{name: "opened", type: "none"},
				{name: "opened_embeded", type: "none" },
				{name: "datatype_id", label: "Тип", type: "select", source: datatypes,change: datatype_changed },
				{name: "size", type: "none" },
				{name: "tablename", type: "none" }
			],
			cssclass: "field"
		}
	]
});
var states={{JSON:states}};
for (var i=0;i<states.length;i++) {
	var st=states[i];
	gstates.ajajform_grid("insertRow",null,{rowClass: "state",cells:[st.name,st.code,st.role_cr_id,st.role_edit_id,st.role_up_to_id,st.role_del_id],id:st.id});
}

var field_groups={{JSON:field_groups}};
for (var i=0;i<field_groups.length;i++) {
	var fg=field_groups[i];
	gfields.ajajform_grid("insertRow",null,{rowClass: "field-group",cells:[fg.name,"",fg.cssclass,fg.opened,fg.opened_embeded,null],id:"fg"+fg.id});
	var fields=fg.fields;
	for (var j=0;j<fields.length;j++) {
		var fi=fields[j];
		insert_field(fi);
	}
}

</script>

